package com.example.itshome.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.itshome.R;
import com.example.itshome.activity.ActivityParticularActivity;
import com.example.itshome.impl.ViewOutlineProviderImpl;
import com.example.itshome.entity.ActivityEntity;

import java.util.List;

public class ActivityListPageRecycleViewAdapter extends RecyclerView.Adapter<ActivityListPageRecycleViewAdapter.ActivityListPageViewHolder> {

    private final List<ActivityEntity> mData;
    private final Context context;
    private Fragment fragment;

    public ActivityListPageRecycleViewAdapter setFragment(Fragment fragment) {
        this.fragment = fragment;
        return this;
    }

    public ActivityListPageRecycleViewAdapter(Context context, List<ActivityEntity> list){
        mData=list;
        this.context=context;
    }
    @NonNull
    @Override
    public ActivityListPageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=View.inflate(context, R.layout.recycle_activity_list_page_item,null);
        view.setOutlineProvider(new ViewOutlineProviderImpl(20));
        view.setClipToOutline(true);
        ActivityListPageViewHolder holder=new ActivityListPageViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ActivityListPageViewHolder holder, int position) {
        ActivityEntity entity= mData.get(position);
        holder.titleTextView.setText(mData.get(position).getTitle());
        holder.organizationTextView.setText(mData.get(position).getOrganizationName());
        holder.timeTextView.setText(mData.get(position).getTime());
        holder.locateTextView.setText(mData.get(position).getLocate());

        Glide.with(context).load(mData.get(position).getPictureSrc()).placeholder(R.drawable.sample_picture).into(holder.pictureImageView);

        holder.wholeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, ActivityParticularActivity.class);
                intent.putExtra("data"
                        ,new String[]{entity.getTitle(), entity.getTime(), entity.getLocate(), entity.getDescription(), entity.getPictureSrc()});
                intent.putExtra("activityId",entity.getId());
                intent.putExtra("isSignUp",entity.isSignUp());
                fragment.startActivityForResult(intent,100);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ActivityListPageViewHolder extends RecyclerView.ViewHolder {

        ImageView pictureImageView;
        TextView titleTextView;
        TextView organizationTextView;
        TextView timeTextView;
        TextView locateTextView;
        View wholeView;
        public ActivityListPageViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView=itemView.findViewById(R.id.txv_recycle_activity_list_title);
            organizationTextView=itemView.findViewById(R.id.txv_recycle_activity_list_organization_name);
            timeTextView=itemView.findViewById(R.id.txv_recycle_activity_list_time);
            locateTextView=itemView.findViewById(R.id.txv_recycle_activity_list_locate);
            wholeView=itemView.findViewById(R.id.layout_recycle_activity_list_whole);
            pictureImageView=itemView.findViewById(R.id.imv_recycle_activity_list_image);
            pictureImageView.setOutlineProvider(new ViewOutlineProviderImpl(20));
            pictureImageView.setClipToOutline(true);
        }
    }
}
